package cn.aibow.cppdocument;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private String linkUrl = "file:///android_asset/cpp/en/index.html";
	private WebView webView;
	
	private ImageButton homeButton;
	private ImageButton backButton;
	private ImageButton goButton;
	private ImageButton refreshButton;
	private ImageButton setButton;
	
	private boolean isExit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		webView = (WebView)findViewById(R.id.main_webview);
		
		homeButton = (ImageButton)findViewById(R.id.main_bottom_home);
		backButton = (ImageButton)findViewById(R.id.main_bottom_back);
		goButton = (ImageButton)findViewById(R.id.main_bottom_go);
		refreshButton = (ImageButton)findViewById(R.id.main_bottom_refresh);
		setButton = (ImageButton)findViewById(R.id.main_bottom_set);
		
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.setWebViewClient(new WebViewClientCallback());
		
		onHomeButton(null);
	}
	
	public void exitApp(){
		if(isExit){
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
			return;
		}
		showToast(getResources().getString(R.string.exit_app));
		isExit = true;
		Handler handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				MainActivity.this.isExit = false;
			}
		};
		handler.sendEmptyMessageDelayed(0, 2000);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_MENU){
			LinearLayout topLayout = (LinearLayout)findViewById(R.id.main_top);
			TableLayout bottomLayout = (TableLayout)findViewById(R.id.main_bottom);
			if(topLayout.getVisibility()==View.GONE){
				topLayout.setVisibility(View.VISIBLE);
				bottomLayout.setVisibility(View.VISIBLE);
			}else{
				topLayout.setVisibility(View.GONE);
				bottomLayout.setVisibility(View.GONE);
			}
			return true;
		}else if(keyCode==KeyEvent.KEYCODE_BACK){
			if(!isExit && webView.canGoBack()){
				onBackButton(null);
			}else{
				exitApp();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}



	public void showToast(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
	public void onHomeButton(View view){
		webView.loadUrl(this.linkUrl);
	}
	
	public void onBackButton(View view){
		if(webView.canGoBack()){
			webView.goBack();
			return;
		}
		showToast(getResources().getString(R.string.webview_not_support_back));
	}
	
	public void onGoButton(View view){
		if(webView.canGoForward()){
			webView.goForward();
			return;
		}
		showToast(getResources().getString(R.string.webview_not_support_go));
	}
	
	public void onRefreshButton(View view){
		webView.reload();
	}
	
	public void onSetButton(View view){
		showToast(getResources().getString(R.string.being_developed));
	}
	
	public void onInfoButton(View view){
		final InfoDialog id = new InfoDialog(this);
		id.show();
		Window win = id.getWindow();
		Button btn = (Button)win.findViewById(R.id.dialog_info_cancel);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				id.cancel();
			}
		});
	}
	
	public void enableButton(){
		homeButton.setEnabled(true);
		backButton.setEnabled(true);
		goButton.setEnabled(true);
		refreshButton.setEnabled(true);
		setButton.setEnabled(true);
	}
	
	public void disableButton(){
		homeButton.setEnabled(true);
		backButton.setEnabled(false);
		goButton.setEnabled(false);
		refreshButton.setEnabled(true);
		setButton.setEnabled(true);
	}
	
	class WebViewClientCallback extends WebViewClient {
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			disableButton();
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			enableButton();
		}

		@Override
		public void doUpdateVisitedHistory(WebView view, String url,boolean isReload) {
			super.doUpdateVisitedHistory(view, url, isReload);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return false;
		}
		
	}
}
